<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="laravel-的-http-请求" tabindex="-1"><a class="header-anchor" href="#laravel-的-http-请求"><span>Laravel 的 HTTP 请求</span></a></h2>
<ul>
<li><a href="#accessing-the-request">获取请求</a>
<ul>
<li><a href="#request-path-and-method">请求路径 &amp; 方法</a></li>
<li><a href="#psr7-requests">PSR-7 请求</a></li>
</ul>
</li>
<li><a href="#input-trimming-and-normalization">输入预处理 &amp; 规范化</a></li>
<li><a href="#retrieving-input">获取输入</a>
<ul>
<li><a href="#old-input">旧输入</a></li>
<li><a href="#cookies">Cookies</a></li>
</ul>
</li>
<li><a href="#files">文件</a>
<ul>
<li><a href="#retrieving-uploaded-files">获取上传文件</a></li>
<li><a href="#storing-uploaded-files">储存上传文件</a></li>
</ul>
</li>
<li><a href="#configuring-trusted-proxies">配置可信代理</a></li>
</ul>
<h2 id="获取请求" tabindex="-1"><a class="header-anchor" href="#获取请求"><span>获取请求</span></a></h2>
<p>要通过依赖注入的方式来获取当前 HTTP 请求的实例，你应该在控制器方法中类型提示 <code v-pre>Illuminate\Http\Request</code>。传入的请求的实例将通过 <a href="https://learnku.com/docs/laravel/5.5/container" target="_blank" rel="noopener noreferrer">服务容器</a> 自动注入：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">UserController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 储存一个新用户。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name">Request</span>  <span class="token parameter">$request</span></span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name">Response</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">store</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$name</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">input</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">//</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="依赖注入-路由参数" tabindex="-1"><a class="header-anchor" href="#依赖注入-路由参数"><span>依赖注入 &amp; 路由参数</span></a></h4>
<p>如果控制器方法要从路由参数中获取数据，则应在其他依赖项之后列出路由参数。例如，如果你的路由是这样定义的：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user/{id}'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'UserController@update'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如下所示使用类型提示 <code v-pre>Illuminate\Http\Request</code>，就可以通过定义控制器方法获取路由参数 <code v-pre>id</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">UserController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 更新指定的用户。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name">Request</span>  <span class="token parameter">$request</span></span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">string</span></span>  <span class="token parameter">$id</span></span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name">Response</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">update</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">,</span> <span class="token variable">$id</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">//</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="通过路由闭包获取请求" tabindex="-1"><a class="header-anchor" href="#通过路由闭包获取请求"><span>通过路由闭包获取请求</span></a></h4>
<p>你也可以在路由闭包中类型提示 <code v-pre>Illuminate\Http\Request</code> 类。服务容器在执行时会自动将当前请求注入到闭包中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">//</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="请求路径-方法" tabindex="-1"><a class="header-anchor" href="#请求路径-方法"><span>请求路径 &amp; 方法</span></a></h3>
<p><code v-pre>Illuminate\Http\Request</code> 实例提供了多种方法来检查应用程序的 HTTP 请求，并继承了 <code v-pre>Symfony\Component\HttpFoundation\Request</code> 类。下面是该类几个有用的方法：</p>
<h4 id="获取请求路径" tabindex="-1"><a class="header-anchor" href="#获取请求路径"><span>获取请求路径</span></a></h4>
<p><code v-pre>path</code> 方法返回请求的路径信息。也就是说，如果传入的请求的目标地址是 <code v-pre>http://domain.com/foo/bar</code>，那么 <code v-pre>path</code> 将会返回 <code v-pre>foo/bar</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$uri</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>is</code> 方法可以验证传入的请求路径和指定规则是否匹配。使用这个方法的时，你也可以传递一个 <code v-pre>*</code> 字符作为通配符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">is</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'admin/*'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">//</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="获取请求的-url" tabindex="-1"><a class="header-anchor" href="#获取请求的-url"><span>获取请求的 URL</span></a></h4>
<p>你可以使用 <code v-pre>url</code> 或 <code v-pre>fullUrl</code> 方法去获取传入请求的完整 URL。<code v-pre>url</code> 方法返回不带有查询字符串的 URL，而 <code v-pre>fullUrl</code> 方法的返回值包含查询字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// Without Query String...</span></span>
<span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">url</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// With Query String...</span></span>
<span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">fullUrl</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="获取请求方法" tabindex="-1"><a class="header-anchor" href="#获取请求方法"><span>获取请求方法</span></a></h4>
<p>对于传入的请求 <code v-pre>method</code> 方法将返回 HTTP 的请求方式。你也可以使用 <code v-pre>isMethod</code> 方法去验证 HTTP 的请求方式与指定规则是否相配：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$method</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">method</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">isMethod</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">//</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="psr-7-请求" tabindex="-1"><a class="header-anchor" href="#psr-7-请求"><span>PSR-7 请求</span></a></h3>
<p><a href="http://www.php-fig.org/psr/psr-7/" target="_blank" rel="noopener noreferrer">PSR-7 标准</a> 规定的 HTTP 消息接口包含了请求和响应。如果你想使用一个 PSR-7 请求来代替一个 Laravel 请求实例，那么你首先要安装几个函数库。Laravel 使用 Symfony 的 HTTP 消息桥接组件将典型的 Laravel 请求和响应转换为 PSR-7 兼容实现：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">composer <span class="token keyword">require</span> symfony<span class="token operator">/</span>psr<span class="token operator">-</span>http<span class="token operator">-</span>message<span class="token operator">-</span>bridge</span>
<span class="line">composer <span class="token keyword">require</span> zendframework<span class="token operator">/</span>zend<span class="token operator">-</span>diactoros</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>安装完这些库后， 就可以在路由闭包或控制器中类型提示请求的接口来获取 PSR-7 请求：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Psr<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Message<span class="token punctuation">\</span>ServerRequestInterface</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">ServerRequestInterface</span> <span class="token variable">$request</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">//</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>{tip} 如果从路由或者控制器返回 PSR-7 响应实例，它会自动转换回 Laravel 响应实例，并由框架显示。</p>
</blockquote>
<h2 id="输入预处理-规范化" tabindex="-1"><a class="header-anchor" href="#输入预处理-规范化"><span>输入预处理 &amp; 规范化</span></a></h2>
<p>默认情况下，Laravel 在应用程序的全局中间件堆栈中包含了 <code v-pre>TrimStrings</code> 和 <code v-pre>ConvertEmptyStringsToNull</code> 两个中间件。这些中间件由 <code v-pre>App\Http\Kernel</code> 类列在堆栈中。它们会自动处理请求上所有传入的字符串字段，并将空的字符串字段转变成 <code v-pre>null</code> 值。这样你就不用再担心路由和控制器中数据规范化的问题。</p>
<p>如果你想停用这些功能，可以从应用程序的中间件堆栈中删除这两个中间件，只需在 <code v-pre>App\Http\Kernel</code> 类的 <code v-pre>$middleware</code> 属性中移除它们。</p>
<h2 id="获取输入" tabindex="-1"><a class="header-anchor" href="#获取输入"><span>获取输入</span></a></h2>
<h4 id="获取所有输入数据" tabindex="-1"><a class="header-anchor" href="#获取所有输入数据"><span>获取所有输入数据</span></a></h4>
<p>你可以使用 <code v-pre>all</code> 方法以 <code v-pre>数组</code> 形式获取到所有输入数据:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$input</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="获取指定输入值" tabindex="-1"><a class="header-anchor" href="#获取指定输入值"><span>获取指定输入值</span></a></h4>
<p>使用几种简单的方法（不需要特别指定哪个 HTTP 动作），就可以访问 <code v-pre>Illuminate\Http\Request</code> 实例中所有的用户输入。也就是说无论是什么样的 HTTP 动作，<code v-pre>input</code> 方法都可以被用来获取用户输入数据：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">input</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以给 <code v-pre>input</code> 方法的第二个参数传入一个默认值。如果请求的输入值不存在请求上，就返回默认值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">input</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Sally'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果传输表单数据中包含「数组」形式的数据，那么可以使用「点」语法来获取数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">input</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'products.0.name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$names</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">input</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'products.*.name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="从查询字符串获取输入" tabindex="-1"><a class="header-anchor" href="#从查询字符串获取输入"><span>从查询字符串获取输入</span></a></h4>
<p>使用 <code v-pre>input</code> 方法可以从整个请求中获取输入数据（包括查询字符串），而 <code v-pre>query</code> 方法可以只从查询字符串中获取输入数据：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">query</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果请求的查询字符串数据不存在，则将返回这个方法的第二个参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">query</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Helen'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以不提供参数调用 <code v-pre>query</code> 方法来以关联数组的形式检索所有查询字符串值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$query</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">query</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="通过动态属性获取输入" tabindex="-1"><a class="header-anchor" href="#通过动态属性获取输入"><span>通过动态属性获取输入</span></a></h4>
<p>你也可以通过 <code v-pre>Illuminate\Http\Request</code> 实例的动态属性来获取用户输入。例如，如果你应用的表单中包含 <code v-pre>name</code> 字段，那么可以像这样访问该字段的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token property">name</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>Laravel 在处理动态属性的优先级是，先在请求的数据中查找，如果没有，再到路由参数中查找。</p>
<h4 id="获取-json-输入信息" tabindex="-1"><a class="header-anchor" href="#获取-json-输入信息"><span>获取 JSON 输入信息</span></a></h4>
<p>如果发送到应用程序的请求数据是 JSON，只要请求的 <code v-pre>Content-Type</code> 标头正确设置为 <code v-pre>application/json</code>，就可以通过 <code v-pre>Input</code> 方法访问 JSON 数据。你甚至可以使用 「点」语法来读取 JSON 数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">input</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user.name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="获取部分输入数据" tabindex="-1"><a class="header-anchor" href="#获取部分输入数据"><span>获取部分输入数据</span></a></h4>
<p>如果你需要获取输入数据的子集，则可以用 <code v-pre>only</code> 和 <code v-pre>except</code> 方法。这两个方法都接收 <code v-pre>数组</code> 或动态列表作为参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$input</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">only</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'username'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'password'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$input</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">only</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'username'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'password'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$input</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">except</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'credit_card'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$input</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">except</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'credit_card'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>{tip} <code v-pre>only</code> 方法会返回所有你指定的键值对，但不会返回请求中不存在的键值对。</p>
</blockquote>
<h4 id="确定是否存在输入值" tabindex="-1"><a class="header-anchor" href="#确定是否存在输入值"><span>确定是否存在输入值</span></a></h4>
<p>要判断请求是否存在某个值，可以使用 <code v-pre>has</code> 方法。如果请求中存在该值，<code v-pre>has</code> 方法就会返回 <code v-pre>true</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">has</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">//</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当提供一个数组作为参数时，<code v-pre>has</code> 方法将确定是否存在数组中所有给定的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">has</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">//</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你想确定请求中是否存在值并且不为空，可以使用 <code v-pre>filled</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">filled</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">//</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="旧输入" tabindex="-1"><a class="header-anchor" href="#旧输入"><span>旧输入</span></a></h3>
<p>Laravel 允许你将本次请求的数据保留到下一次请求发送前。如果第一次请求的表单不能通过验证，就可以使用这个功能重新填充表单。但是，如果你使用了 Laravel 的 <a href="https://learnku.com/docs/laravel/5.5/validation" target="_blank" rel="noopener noreferrer">验证功能</a>，你就不需要在手动实现这些方法，因为 Laravel 内置的验证工具会自动调用他们。</p>
<h4 id="将输入闪存至-session" tabindex="-1"><a class="header-anchor" href="#将输入闪存至-session"><span>将输入闪存至 Session</span></a></h4>
<p><code v-pre>Illuminate\Http\Request</code> 的 <code v-pre>flash</code> 方法会将当前输入的数据存进 <a href="https://learnku.com/docs/laravel/5.5/session" target="_blank" rel="noopener noreferrer">session</a> 中，以便在用户下次发送请求到应用程序之前可以使用它们：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">flash</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你也可以使用 <code v-pre>flashOnly</code> 和 <code v-pre>flashExcept</code> 方法将请求数据的一部分闪存到 session。这些方法对敏感信息（例如密码）的保护非常有用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">flashOnly</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'username'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">flashExcept</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'password'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="闪存输入后重定向" tabindex="-1"><a class="header-anchor" href="#闪存输入后重定向"><span>闪存输入后重定向</span></a></h4>
<p>你可能需要把输入闪存到 session 然后重定向到上一页，这时只需要在重定向方法后加上 <code v-pre>withInput</code> 即可：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'form'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">withInput</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'form'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">withInput</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">except</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'password'</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="获取旧输入" tabindex="-1"><a class="header-anchor" href="#获取旧输入"><span>获取旧输入</span></a></h4>
<p>若要获取上一次请求中闪存的输入，则可以使用 <code v-pre>Request</code> 实例中的 <code v-pre>old</code> 方法。<code v-pre>old</code> 方法会从 <a href="https://learnku.com/docs/laravel/5.5/session" target="_blank" rel="noopener noreferrer">Session</a> 取出之前被闪存的输入数据：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$username</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">old</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'username'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>Laravel 也提供了全局辅助函数 <code v-pre>old</code>。如果你要在 <a href="https://learnku.com/docs/laravel/5.5/blade" target="_blank" rel="noopener noreferrer">Blade 模板</a> 中显示旧的输入，使用 <code v-pre>old</code> 会更加方便。如果给定字段没有旧的输入，则返回 <code v-pre>null</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">&lt;</span>input type<span class="token operator">=</span><span class="token string double-quoted-string">"text"</span> name<span class="token operator">=</span><span class="token string double-quoted-string">"username"</span> value<span class="token operator">=</span><span class="token string double-quoted-string">"{{ old('username') }}"</span><span class="token operator">></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="cookies" tabindex="-1"><a class="header-anchor" href="#cookies"><span>Cookies</span></a></h3>
<h4 id="从请求中获取-cookie" tabindex="-1"><a class="header-anchor" href="#从请求中获取-cookie"><span>从请求中获取 Cookie</span></a></h4>
<p>Laravel 框架创建的每个 cookie 都会被加密并使用验证码进行签名，这意味着如果客户端更改了它们，便视为无效。若要从请求中获取 cookie 值，你可以在 <code v-pre>Illuminate\Http\Request</code> 实例上使用 <code v-pre>cookie</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">cookie</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="将-cookies-附加到响应" tabindex="-1"><a class="header-anchor" href="#将-cookies-附加到响应"><span>将 Cookies 附加到响应</span></a></h4>
<p>你可以使用 <code v-pre>cookie</code> 方法将 cookie 附加到传出的 <code v-pre>Illuminate\Http\Response</code> 实例。你需要传递 Cookie 名称、值、以及有效期（分钟）到这个方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">response</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">cookie</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">,</span> <span class="token variable">$minutes</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>cookie</code> 方法还接受一些不太频繁使用的参数。通常，这些参数与 PHP 原生 <a href="http://php.net/manual/en/function.setcookie.php" target="_blank" rel="noopener noreferrer">setcookie</a> 方法的参数具有相同的目的和意义：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">response</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">cookie</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">,</span> <span class="token variable">$minutes</span><span class="token punctuation">,</span> <span class="token variable">$path</span><span class="token punctuation">,</span> <span class="token variable">$domain</span><span class="token punctuation">,</span> <span class="token variable">$secure</span><span class="token punctuation">,</span> <span class="token variable">$httpOnly</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="生成-cookie-实例" tabindex="-1"><a class="header-anchor" href="#生成-cookie-实例"><span>生成 Cookie 实例</span></a></h4>
<p>如果你想要在一段时间以后生成一个可以给定 <code v-pre>Symfony\Component\HttpFoundation\Cookie</code> 的响应实例，你可以使用全局辅助函数 <code v-pre>cookie</code>。除非此 cookie 被附加到响应实例，否则不会发送回客户端：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$cookie</span> <span class="token operator">=</span> <span class="token function">cookie</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">,</span> <span class="token variable">$minutes</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token function">response</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">cookie</span><span class="token punctuation">(</span><span class="token variable">$cookie</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="文件" tabindex="-1"><a class="header-anchor" href="#文件"><span>文件</span></a></h2>
<h3 id="获取上传文件" tabindex="-1"><a class="header-anchor" href="#获取上传文件"><span>获取上传文件</span></a></h3>
<p>你可以使用 <code v-pre>file</code> 方法或使用动态属性从 <code v-pre>Illuminate\Http\Request</code> 实例中访问上传的文件。该 <code v-pre>file</code> 方法返回一个 <code v-pre>Illuminate\Http\UploadedFile</code> 类的实例，该类继承了PHP 的 <code v-pre>SplFileInfo</code> 类的同时也提供了各种与文件交互的方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$file</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">file</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'photo'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$file</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token property">photo</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以使用 <code v-pre>hasFile</code> 方法确认请求中是否存在文件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">hasFile</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'photo'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">//</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="验证成功上传" tabindex="-1"><a class="header-anchor" href="#验证成功上传"><span>验证成功上传</span></a></h4>
<p>除了检查上传的文件是否存在外，你也可以通过 <code v-pre>isValid</code> 方法验证上传的文件是否有效：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">file</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'photo'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isValid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">//</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="文件路径-扩展名" tabindex="-1"><a class="header-anchor" href="#文件路径-扩展名"><span>文件路径 &amp; 扩展名</span></a></h4>
<p><code v-pre>UploadedFile</code> 类还包含访问文件的完整路径及其扩展名方法。<code v-pre>extension</code> 方法会根据文件内容判断文件的扩展名。该扩展名可能会和客户端提供的扩展名不同：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token property">photo</span><span class="token operator">-></span><span class="token function">path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$extension</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token property">photo</span><span class="token operator">-></span><span class="token function">extension</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="其它文件方法" tabindex="-1"><a class="header-anchor" href="#其它文件方法"><span>其它文件方法</span></a></h4>
<p><code v-pre>UploadedFile</code> 实例上还有许多可用的方法，可以查看该类的 <a href="http://api.symfony.com/3.0/Symfony/Component/HttpFoundation/File/UploadedFile.html" target="_blank" rel="noopener noreferrer">API 文档</a> 了解这些方法的详细信息。</p>
<h3 id="存储上传文件" tabindex="-1"><a class="header-anchor" href="#存储上传文件"><span>存储上传文件</span></a></h3>
<p>要存储上传的文件，先配置好 <a href="https://learnku.com/docs/laravel/5.5/filesystem" target="_blank" rel="noopener noreferrer">文件系统</a>。你可以使用 <code v-pre>UploadedFile</code> 的 <code v-pre>store</code> 方法把上传文件移动到你的某个磁盘上，该文件可能是本地文件系统中的一个位置，甚至像 Amazon S3 这样的云存储位置。</p>
<p><code v-pre>store</code> 方法接受相对于文件系统配置的存储文件根目录的路径。这个路径不能包含文件名，因为系统会自动生成唯一的 ID 作为文件名。</p>
<p><code v-pre>store</code> 方法还接受可选的第二个参数，用于存储文件的磁盘名称。这个方法会返回相对于磁盘根目录的文件路径：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token property">photo</span><span class="token operator">-></span><span class="token function">store</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'images'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token property">photo</span><span class="token operator">-></span><span class="token function">store</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'images'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'s3'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你不想自动生成文件名，那么可以使用 <code v-pre>storeAs</code> 方法，它接受路径、文件名和磁盘名作为其参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token property">photo</span><span class="token operator">-></span><span class="token function">storeAs</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'images'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'filename.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token property">photo</span><span class="token operator">-></span><span class="token function">storeAs</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'images'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'filename.jpg'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'s3'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="配置可信代理" tabindex="-1"><a class="header-anchor" href="#配置可信代理"><span>配置可信代理</span></a></h2>
<p>如果你的应用程序运行在失效的 TLS / SSL 证书的负载均衡器后，你可能会注意到你的应用程序有时不能生成 HTTPS 链接。通常这是因为你的应用程序正在从端口 80 上的负载平衡器转发流量，却不知道是否应该生成安全链接。</p>
<p>解决这个问题需要在 Laravel 应用程序中包含 <code v-pre>App\Http\Middleware\TrustProxies</code> 中间件，这使得你可以快速自定义应用程序信任的负载均衡器或代理。你信任的代理应该保存在这个中间件的 <code v-pre>$proxies</code> 数组中。除了配置可信代理之外，你还可以配置代理发送包含原始请求信息的请求头：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Middleware</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Fideloper<span class="token punctuation">\</span>Proxy<span class="token punctuation">\</span>TrustProxies</span> <span class="token keyword">as</span> Middleware<span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">TrustProxies</span> <span class="token keyword">extends</span> <span class="token class-name">Middleware</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 这个应用程序的可信代理列表</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">array</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">protected</span> <span class="token variable">$proxies</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'192.168.1.1'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'192.168.1.2'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 当前代理响应头映射关系</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">array</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">protected</span> <span class="token variable">$headers</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token class-name static-context">Request</span><span class="token operator">::</span><span class="token constant">HEADER_FORWARDED</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'FORWARDED'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token class-name static-context">Request</span><span class="token operator">::</span><span class="token constant">HEADER_X_FORWARDED_FOR</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'X_FORWARDED_FOR'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token class-name static-context">Request</span><span class="token operator">::</span><span class="token constant">HEADER_X_FORWARDED_HOST</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'X_FORWARDED_HOST'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token class-name static-context">Request</span><span class="token operator">::</span><span class="token constant">HEADER_X_FORWARDED_PORT</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'X_FORWARDED_PORT'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token class-name static-context">Request</span><span class="token operator">::</span><span class="token constant">HEADER_X_FORWARDED_PROTO</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'X_FORWARDED_PROTO'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="信任所有代理" tabindex="-1"><a class="header-anchor" href="#信任所有代理"><span>信任所有代理</span></a></h4>
<p>如果你使用 Amazon AWS 或其他的「云」负载均衡器提供程序，你可能不知道负载均衡器的实际 IP 地址。在这种情况下，你可以使用 <code v-pre>**</code> 来信任所有代理：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 这个应用程序的可信代理列表</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">array</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token variable">$proxies</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'**'</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="译者署名" tabindex="-1"><a class="header-anchor" href="#译者署名"><span>译者署名</span></a></h2>
<table>
<thead>
<tr>
<th>用户名</th>
<th>头像</th>
<th>职能</th>
<th>签名</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="https://learnku.com/users/5350" target="_blank" rel="noopener noreferrer">@JokerLinly</a></td>
<td><img src="https://cdn.learnku.com/uploads/avatars/5350_1481857380.jpg" alt="5350_1481857380.jpg"></td>
<td>Review</td>
<td>Stay Hungry. Stay Foolish.</td>
</tr>
</tbody>
</table>
<hr>
<blockquote>
<p>{note} 欢迎任何形式的转载，但请务必注明出处，尊重他人劳动共创开源社区。</p>
<p>转载请注明：本文档由 Laravel China 社区 <a href="https://laravel-china.org/" target="_blank" rel="noopener noreferrer">laravel-china.org</a> 组织翻译，详见 <a href="https://learnku.com/laravel/t/5756/laravel-55-document-translation-call-come-and-join-the-translation" target="_blank" rel="noopener noreferrer">翻译召集帖</a>。</p>
<p>文档永久地址： <a href="https://learnku.com/docs/laravel" target="_blank" rel="noopener noreferrer">《Laravel 中文文档》</a></p>
</blockquote>
</div></template>


